/**
 * 
 */
package cn.com.gxetc.assistant.web;

import java.util.Comparator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import cn.com.gxetc.assistant.dto.Todo;
import cn.com.gxetc.assistant.dto.TodoCount;
import cn.com.gxetc.assistant.dto.TodoPublish;
import cn.com.gxetc.assistant.service.TodoService;
import cn.com.gxetc.assistant.utils.JwtUtils;

/**
 * @author yfm
 *
 */
@RestController
@RequestMapping("/todo/")
public class TodoController {

	@Autowired
	TodoService toService;

	@Autowired
	private JwtUtils jwtUtils;

	@ResponseBody
	@RequestMapping("/mine")
	public List<Todo> findMyTodo(@CookieValue(value = "token",required=false) String token) {
		Assert.notNull(token, "userId cannot be null");

		return toService.findByEmployeeId(jwtUtils.getClaimByToken(token));
	}
	
	@ResponseBody
	@RequestMapping("/mine/count")
	public TodoCount countTodo(@CookieValue(value = "token",required=false) String token) {
		Assert.notNull(token, "userId cannot be null");
		List<TodoCount> todos=toService.countByEmployeeIds(jwtUtils.getClaimByToken(token));
		if(todos.size()>0)
			return todos.get(0);
		
		return new TodoCount(0);
	}
	@ResponseBody
	@RequestMapping("/publish")
	public List<TodoPublish> publish(Long timeout) {
		if (null == timeout) {
			timeout = 300L;
		}
		List<TodoPublish> todos = toService.findPublish(timeout);
		todos.sort(new Comparator<TodoPublish>() {
			@Override
			public int compare(TodoPublish a, TodoPublish b) {
				return b.getCount() - a.getCount();
			}
		});

		return todos;
	}

}
